package com.yao.component.pictureselector;
/**
 * 
 * @author Aizaz AZ
 *
 */

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.yao.component.pictureselector.photoview.PhotoViewAttacher;
import com.yao.component.pictureselector.widget.AlertMsgDialog;
import com.yao.component.pictureselector.widget.PhotoViewPage;
import com.yao.sdk.glide.GlideUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class PreviewDeleteActivity extends Activity implements OnPageChangeListener, OnClickListener {

   private PhotoViewPage mViewPager;
   private RelativeLayout layoutTop;
   private ImageView btnBack,delete;
	private TextView positionTv;
   protected List<String> photosPath;
   protected int current;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
//		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏

       setContentView(R.layout.activity_photodelete);
	   positionTv=(TextView)findViewById(R.id.position);
       layoutTop = (RelativeLayout) findViewById(R.id.layout_top_app);
       btnBack = (ImageView) findViewById(R.id.btn_back_app);
       delete= (ImageView) findViewById(R.id.delete);
       mViewPager = (PhotoViewPage) findViewById(R.id.vp_base_app);
	   delete.setOnClickListener(this);
       btnBack.setOnClickListener(this);
       mViewPager.addOnPageChangeListener(this);
       mViewPager.setOffscreenPageLimit(3);
       overridePendingTransition(R.anim.activity_alpha_action_in, 0); // 渐入效果
       initData();
   }
   public  void initData(){
       Bundle bundle=getIntent().getExtras();
       photosPath=bundle.getStringArrayList("paths");
       current=bundle.getInt("position", 0);
       bindData();
   }
   /** 绑定数据，更新界面 */
   protected void bindData() {
	   upDataPosition();
       mViewPager.setAdapter(mPagerAdapter);
       mViewPager.setCurrentItem(current);

   }
	private void upDataPosition(){
		positionTv.setText((current+1)+"/"+photosPath.size());
	}
   private PagerAdapter mPagerAdapter = new PagerAdapter() {
       private int mChildCount = 0;
       @Override
       public int getCount() {
           if (photosPath == null) {
               return 0;
           } else {
               return photosPath.size();
           }
       }
       @Override
       public View instantiateItem(final ViewGroup container, final int position) {
           View v;
           int screenWidth = CommonUtil.getScreenWidth(container.getContext());
           if (photosPath.get(position).contains(".gif")||
                   photosPath.get(position).contains(".GIF")) {
               v = LayoutInflater.from(container.getContext()).inflate(R.layout.preview_photo_item, null);
               MyPhotoView photoView = (MyPhotoView) v.findViewById(R.id.photo_view);
               Bitmap map = BitmapFactory.decodeFile(photosPath.get(position));
               int height=0;
               if (map!=null&&map.getWidth()!=0){
                   height = map.getHeight() * screenWidth / map.getWidth();
               }
               LinearLayout.LayoutParams lp;
               if (height > CommonUtil.getScreenHeight(container.getContext())) {
                   lp = new LinearLayout.LayoutParams(screenWidth, height);
//                ((ScrollView)v.findViewById(R.id.scroll_view)).setFillViewport(false);
               } else {
                   lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
               }

               photoView.setLayoutParams(lp);
               photoView.setOnViewTapListener(viewTapListener);
               GlideUtil.getInstance().loadFile(PreviewDeleteActivity.this, new File(photosPath.get(position)), photoView, true, false);
           }else {
               v = LayoutInflater.from(container.getContext()).inflate(R.layout.preview_photo_item2, null);
               final SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)v.findViewById(R.id.imageView);

               Bitmap map=BitmapFactory.decodeFile(photosPath.get(position));
               imageView.setDoubleTapZoomDuration(300);
               if (map!=null&&map.getWidth()!=0){
                   float scale=(float) screenWidth/map.getWidth()*4;
                   imageView.setMaxScale(scale);
                   imageView.setDoubleTapZoomScale(scale);
                   int height =map.getHeight() * screenWidth /map.getWidth();
                   imageView.setLayoutParams(new ScrollView.LayoutParams(screenWidth,height));
               }

               imageView.setImage(ImageSource.uri(photosPath.get(position)));
               imageView.setOnClickListener(imgClickListener);
           }
           container.addView(v);
           return v;
       }
//       @Override
//       public View instantiateItem(final ViewGroup container, final int position) {
//
//           MyPhotoView photoView = new MyPhotoView(container.getContext());
//           GlideUtil.getInstance().loadFile(PreviewDeleteActivity.this,new File(photosPath.get(position)),photoView,true);
////           photoView.loadImage("file://"+photosPath.get(position));
//           photoView.setOnViewTapListener(viewTapListener);
//           // Now just add PhotoView to ViewPager and return it
//           container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//
//           return photoView;
//       }

       @Override
       public void destroyItem(ViewGroup container, int position, Object object) {
           container.removeView((View) object);
       }

       @Override
       public boolean isViewFromObject(View view, Object object) {
           return view == object;
       }

       public int getItemPosition(Object object){
           return POSITION_NONE;
       }

   };
   protected boolean isUp;

   @Override
   public void onClick(View v) {
       int id=v.getId();
       if (id==R.id.btn_back_app){
           returnResult();
       }else if(id== R.id.delete){
           if (photosPath.size()==1){
               photosPath.clear();
               returnResult();
           }else {
               if (photosPath.size()-1>=current){
                   photosPath.remove(current);
               }

               mPagerAdapter.notifyDataSetChanged();
           }

       }



   }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        returnResult();
    }

    @Override
   public void onPageScrollStateChanged(int arg0) {
        Log.e("zwj","...onPageScrollStateChanged");
   }

   @Override
   public void onPageScrolled(int arg0, float arg1, int arg2) {
       Log.e("zwj","...onPageScrolled");
       upDataPosition();
   }

   @Override
   public void onPageSelected(int arg0) {
       current = arg0;

   }

    public void returnResult(){
        Intent intent = new Intent();
        intent.setAction("com.yao.component.pictureselector.deleteimage");
        intent.putStringArrayListExtra("result_image", (ArrayList<String>) photosPath);
        PreviewDeleteActivity.this.sendBroadcast(intent);
        PreviewDeleteActivity.this.finish();
    }
    private OnClickListener imgClickListener=new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!isUp) {
                new AnimationUtil(getApplicationContext(), R.anim.top_translate_up)
                        .setInterpolator(new LinearInterpolator()).setFillAfter(true).startAnimation(layoutTop);
                isUp = true;
            } else {
                new AnimationUtil(getApplicationContext(), R.anim.top_translate_down)
                        .setInterpolator(new LinearInterpolator()).setFillAfter(true).startAnimation(layoutTop);
                isUp = false;
            }
        }
    };
   private PhotoViewAttacher.OnViewTapListener viewTapListener=new PhotoViewAttacher.OnViewTapListener() {
       @Override
       public void onViewTap(View view, float x, float y) {
           if (!isUp) {
               new AnimationUtil(getApplicationContext(), R.anim.top_translate_up)
                       .setInterpolator(new LinearInterpolator()).setFillAfter(true).startAnimation(layoutTop);
               isUp = true;
           } else {
               new AnimationUtil(getApplicationContext(), R.anim.top_translate_down)
                       .setInterpolator(new LinearInterpolator()).setFillAfter(true).startAnimation(layoutTop);
               isUp = false;
           }
       }
   };
}
